package com.yuncheng.spcyApi.spcy;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.collection.CollectionUtil;
import org.apache.commons.collections.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.spcyApi.alog.annotation.Log;
import com.yuncheng.spcyApi.busService.InitWjjlService;
import com.yuncheng.spcyApi.constant.myd.SpcyMyddcConstant;
import com.yuncheng.spcyApi.constant.wjml.WjmlConstant;
import com.yuncheng.spcyApi.entity.*;
import com.yuncheng.spcyApi.mapper.SpcySqsxLjzlMydBasicMapper;
import com.yuncheng.spcyApi.service.*;
import com.yuncheng.spcyApi.spcyService.SpcyMyddcService;
import com.yuncheng.vo.HttpResult;
import com.yuncheng.spcyApi.vo.common.HttpResultVo;
import com.yuncheng.spcyApi.vo.common.PageVo;
import com.yuncheng.spcyApi.vo.sqsx.LjzlMydBasicVo;
import com.yuncheng.spcyApi.vo.common.Result;
import io.swagger.annotations.ApiImplicitParam;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


/**
 * @Description: 审评查验-检查员廉洁自律和满意度基本信息
 * @Author: yuncheng
 * @Date: 2023-04-18 spcySqsxLjzlMydBasic
 * @Version: V1.0
 */
@Api(tags = "审评查验-检查员廉洁自律和满意度基本信息接口")
@RestController
@RequestMapping("/api/spzx-spcy/myd")
public class SpcyMyddcController {

    private static final Logger log = LoggerFactory.getLogger(SpcyMyddcController.class);

    @Resource
    private ISpcySqsxLjzlMydBasicService spcySqsxLjzlMydBasicService;

    @Resource
    private ISpcySqsxLjzlMydZxpjService spcySqsxLjzlMydZxpjService;

    @Resource
    private ISpcySqsxLjzlMydHcypjService spcySqsxLjzlMydHcypjService;

    @Resource
    private SpcySqsxLjzlMydBasicMapper spcySqsxLjzlMydBasicMapper;

    @Resource
    private InitWjjlService initWjjlService;

    @Resource
    private SpcyMyddcService spcyMyddcService;

    @Log(title = "获取廉洁自律和满意度问卷统计-分页查询")
    @ApiOperation(value = "获取廉洁自律和满意度问卷统计-分页查询")
    @GetMapping(value = "queryLjzlMydPageList")
    public HttpResult queryLjzlMydPageList(LjzlMydBasicVo query, PageVo pageVo){
        Page<LjzlMydBasicVo> page = new Page<>(pageVo.getPageNo(),pageVo.getPageSize());
        Page<LjzlMydBasicVo> basicVoPage = spcySqsxLjzlMydBasicMapper.queryPageList(page, query);

        return HttpResult.ok(basicVoPage);
    }

    @Log(title = "查询满意度是否提交-根据申请事项id")
    @ApiOperation(value = "查询满意度是否提交-根据申请事项id")
    @GetMapping(value = "queryMydState")
    public HttpResult queryMydState(@RequestParam(value = "sqsxid",defaultValue = "") String sqsxid){
        if (StringUtils.isBlank(sqsxid)){
            return HttpResult.error("事项参数为空！");
        }

        String flag = SpcyMyddcConstant.LJZL_MYD_SFTJ_NO;
        List<String> list = spcySqsxLjzlMydBasicMapper.selectFsftjByFsqsxid(sqsxid);
        if (CollectionUtil.isNotEmpty(list) && StringUtils.isNotBlank(list.get(0))){
            flag = list.get(0);
        }

        return HttpResultVo.HttpResultSave(flag, "操作成功");
    }

    @Log(title = "获取初始化廉洁自律和满意度调查问卷")
    @ApiOperation(value = "获取初始化廉洁自律和满意度调查问卷")
    @GetMapping(value = "querySqsxLjzlMydObject")
    public HttpResult querySqsxLjzlMydObject(@RequestParam(value = "sqsxid",defaultValue = "") String sqsxid){
        if (StringUtils.isBlank(sqsxid)){
            return HttpResult.error("事项参数为空！");
        }

        Result result = spcyMyddcService.initHcyLjzlMydBasic(sqsxid);
        if (!result.isSuccess()){
            return HttpResult.error(result.getMessage());
        }

        return HttpResult.ok(result.getResult());
    }

    @Log(title = "获取廉洁自律和满意度调查问卷详情")
    @ApiOperation(value = "获取廉洁自律和满意度调查问卷详情")
    @GetMapping(value = "querySqsxLjzlMydXq")
    public HttpResult querySqsxLjzlMydXq(@RequestParam(value = "sqsxid",defaultValue = "") String sqsxid){
        if (StringUtils.isBlank(sqsxid)){
            return HttpResult.error("申请事项参数丢失，请联系管理员");
        }

        return HttpResult.ok(spcyMyddcService.querySqsxLjzlMydXq(sqsxid));
    }

    @Log(title = "获取满意度问卷详情-根据id")
    @ApiOperation(value = "获取满意度问卷详情-根据id")
    @GetMapping(value = "getMydInfoById")
    public HttpResult getMydInfoById(@RequestParam(value = "id",defaultValue = "") String id){
        if (StringUtils.isBlank(id)){
            return HttpResult.error("参数为空！");
        }

        return HttpResult.ok(spcyMyddcService.getMydInfoById(id));
    }

    @Log(title = "提交廉洁自律和满意度调查问卷")
    @ApiOperation(value = "提交廉洁自律和满意度调查问卷")
    @ApiImplicitParam(name = "id",value = "廉洁自律id",required = true)
    @GetMapping(value = "tjLjzlMydWj")
    public HttpResult tjLjzlMydWj(@RequestParam(value = "id",defaultValue = "") String id){
        if (StringUtils.isBlank(id)){
            return HttpResult.error("主要参数不能为空");
        }

        SpcySqsxLjzlMydBasic ljzl = spcySqsxLjzlMydBasicService.getById(id);
        if (ljzl == null){
            return HttpResult.error("没有想匹配的问卷信息！");
        }

        if (StringUtils.isNotBlank(ljzl.getFsftj()) && ljzl.getFsftj().equals(SpcyMyddcConstant.LJZL_MYD_SFTJ_YES)){
            return HttpResult.error("问卷已经提交过来无法再次提交！");
        }

        ljzl.setFsftj(SpcyMyddcConstant.LJZL_MYD_SFTJ_YES);
        if (ljzl.getFtxrq() == null) {
            ljzl.setFtxrq(new Date());
        }
        ljzl.setUpdateTime(new Date());
        spcySqsxLjzlMydBasicService.updateById(ljzl);

        return HttpResult.ok("提交成功");
    }

    @Log(title = "检查员廉洁自律和满意度基本信息-修改")
    @ApiOperation(value = "检查员廉洁自律和满意度基本信息-修改")
    @PostMapping(value = "/editLjzlMyd")
    public HttpResult editLjzlMyd(@RequestBody SpcySqsxLjzlMydBasic bean) {
        if (StringUtils.isBlank(bean.getId())){
            return HttpResult.error("主要参数丢失！");
        }

        LambdaUpdateWrapper<SpcySqsxLjzlMydBasic> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SpcySqsxLjzlMydBasic::getId, bean.getId());

        SpcySqsxLjzlMydBasic updateBean = new SpcySqsxLjzlMydBasic();

//        if (StringUtils.isNotBlank(bean.getFgzdw())) {
            updateBean.setFgzdw(bean.getFgzdw());
//        }
//        if (StringUtils.isNotBlank(bean.getFlxdh())) {
            updateBean.setFlxdh(bean.getFlxdh());
//        }
//        if (StringUtils.isNotBlank(bean.getFqyfzr())) {
            updateBean.setFqyfzr(bean.getFqyfzr());
//        }
//        if (StringUtils.isNotBlank(bean.getFxz())) {
            updateBean.setFxz(bean.getFxz());
//        }
//        if (StringUtils.isNotBlank(bean.getFqtxz())) {
            updateBean.setFqtxz(bean.getFqtxz());
//        }
//        if (StringUtils.isNotBlank(bean.getFsfxzqtxz())) {
            updateBean.setFsfxzqtxz(bean.getFsfxzqtxz());
//        }
        updateBean.setFyjhyq(bean.getFyjhyq());
        if (bean.getFtxrq() != null) {
            updateBean.setFtxrq(bean.getFtxrq());
        }
        spcySqsxLjzlMydBasicService.update(updateBean, updateWrapper);

        /*SpcySqsxLjzlMydBasic updateLjzl = spcySqsxLjzlMydBasicService.getById(bean.getId());
        if (updateLjzl == null){
            return HttpResult.error("当前数据不存在，请刷新重试");
        }

        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        try {
            BeanUtils.populate(updateLjzl, BeanUtils.describe(bean));
            if (bean.getFtxrq() != null) {
                updateLjzl.setFtxrq(bean.getFtxrq());
            }
        } catch (IllegalAccessException e) {
            log.error("保存出错【IllegalAccessException】- {}",e.getMessage());
        } catch (InvocationTargetException e) {
            log.error("保存出错【InvocationTargetException】- {}",e.getMessage());
        } catch (NoSuchMethodException e) {
            log.error("保存出错【NoSuchMethodException】- {}",e.getMessage());
        }
        spcySqsxLjzlMydBasicService.updateById(updateLjzl);*/

        return HttpResult.ok("保存成功");
    }

    @Log(title = "对检查员组评价-修改")
    @ApiOperation(value = "对检查员组评价-修改")
    @PostMapping(value = "updateHcyPj")
    public HttpResult updateHcyPj(@RequestBody SpcySqsxLjzlMydHcypj bean){
        if (StringUtils.isBlank(bean.getId())){
            return HttpResult.error("主要参数为空！");
        }

        SpcySqsxLjzlMydHcypj updateBean = new SpcySqsxLjzlMydHcypj();

//        if (StringUtils.isNotBlank(bean.getFywnl())) {
            updateBean.setFywnl(bean.getFywnl());
//        }

//        if (StringUtils.isNotBlank(bean.getFzxqk())) {
            updateBean.setFzxqk(bean.getFzxqk());
//        }

//        if (StringUtils.isNotBlank(bean.getFzhpj())) {
            updateBean.setFzhpj(bean.getFzhpj());
//        }

        updateBean.setFbz(bean.getFbz());

        LambdaUpdateWrapper<SpcySqsxLjzlMydHcypj> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SpcySqsxLjzlMydHcypj::getId, bean.getId());
        spcySqsxLjzlMydHcypjService.update(updateBean, updateWrapper);


        /*SpcySqsxLjzlMydHcypj updateBean = spcySqsxLjzlMydHcypjService.getById(bean.getId());
        if (updateBean == null){
            return HttpResult.error("没有相匹配的数据！");
        }
        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        try {
            BeanUtils.populate(updateBean,BeanUtils.describe(bean));
        } catch (IllegalAccessException e) {
            log.error("保存出错【IllegalAccessException】- {}",e.getMessage());
        } catch (InvocationTargetException e) {
            log.error("保存出错【InvocationTargetException】- {}",e.getMessage());
        } catch (NoSuchMethodException e) {
            log.error("保存出错【NoSuchMethodException】- {}",e.getMessage());
        }
        spcySqsxLjzlMydHcypjService.updateById(updateBean);*/

        return HttpResult.ok("保存成功");
    }

    @Log(title = "修改中心的评价-通过id")
    @ApiOperation(value = "修改中心的评价-通过id")
    @PostMapping(value = "updateZxPjById")
    public HttpResult updateZxPjById(@RequestBody SpcySqsxLjzlMydZxpj bean){
        if (StringUtils.isBlank(bean.getId())){
            return HttpResult.error("主要参数为空！");
        }

        LambdaUpdateWrapper<SpcySqsxLjzlMydZxpj> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SpcySqsxLjzlMydZxpj::getId, bean.getId());

        SpcySqsxLjzlMydZxpj updateBean = new SpcySqsxLjzlMydZxpj();
        updateBean.setFpjjg(bean.getFpjjg());
        spcySqsxLjzlMydZxpjService.update(updateBean, updateWrapper);

        return HttpResult.ok("保存成功");
    }

    @Log(title = "下载满意度调查表PDF文件-根据问卷id")
    @ApiOperation(value = "下载满意度调查表PDF文件-根据问卷id")
    @GetMapping(value = "downMydTemplatepPdfFile")
    public void downMydTemplatepPdfFile(@RequestParam(value = "id",defaultValue = "") String id,
                                        HttpServletResponse response) throws IOException {
        if (StringUtils.isBlank(id)){
            response.setContentType("text/html");
            response.setCharacterEncoding("UTF-8");
            PrintWriter writer = response.getWriter();
            writer.println("参数为空！");
            writer.close(); // 不要忘记关闭PrintWriter

            return;
        }

        // 验证是否存在相关满意度调查问卷
        SpcySqsxLjzlMydBasic myd = spcySqsxLjzlMydBasicService.getById(id);
        if (myd == null){
            response.setContentType("text/html");
            response.setCharacterEncoding("UTF-8");
            PrintWriter writer = response.getWriter();
            writer.println("没有相匹配的数据！");
            writer.close(); // 不要忘记关闭PrintWriter

            return;
        }

        spcyMyddcService.downMydTableTemplatePdf(myd, true, response);
    }

//    @Log(title = "下载满意度调查表文档-根据申请事项id")
//    @ApiOperation(value = "下载满意度调查表文档-根据申请事项id")
    @GetMapping(value = "downMydTemplateFile")
    public void downMydTemplateFile(@RequestParam(value = "sqsxid",defaultValue = "") String sqsxid,
                                    HttpServletResponse response) throws IOException {
        if (StringUtils.isBlank(sqsxid)){
            response.setContentType("text/html");
            response.setCharacterEncoding("UTF-8");
//            response.getWriter().write("没有相匹配的事项参数！");
            PrintWriter writer = response.getWriter();
            writer.println("没有相匹配的事项参数！");
            writer.close(); // 不要忘记关闭PrintWriter

            return;
        }

        // 验证是否存在相关满意度调查问卷
        List<SpcySqsxLjzlMydBasic> mydList = spcySqsxLjzlMydBasicService.list(new LambdaQueryWrapper<SpcySqsxLjzlMydBasic>()
                .eq(SpcySqsxLjzlMydBasic::getFsqsxid, sqsxid));
        if (CollectionUtils.isNotEmpty(mydList)) {
            spcyMyddcService.downMydTableTemplate(mydList.get(0), response);

        } else {
            initWjjlService.downloadSqsxFileMb("", sqsxid, WjmlConstant.khmyddc, response);
        }

    }

//    @Log(title = "下载满意度调查表文档-根据问卷id")
//    @ApiOperation(value = "下载满意度调查表文档-根据问卷id")
    @GetMapping(value = "downMydMbFileById")
    public void downMydMbFileById(@RequestParam(value = "id",defaultValue = "") String id,
                                    HttpServletResponse response) throws IOException {
        if (StringUtils.isBlank(id)){
            response.setContentType("text/html");
            response.setCharacterEncoding("UTF-8");
            PrintWriter writer = response.getWriter();
            writer.println("参数为空！");
            writer.close(); // 不要忘记关闭PrintWriter

            return;
        }

        // 验证是否存在相关满意度调查问卷
        SpcySqsxLjzlMydBasic myd = spcySqsxLjzlMydBasicService.getById(id);
        if (myd == null){
            response.setContentType("text/html");
            response.setCharacterEncoding("UTF-8");
            PrintWriter writer = response.getWriter();
            writer.println("没有相匹配的数据！");
            writer.close(); // 不要忘记关闭PrintWriter

            return;
        }

        spcyMyddcService.downMydTableTemplatePdf(myd, false, response);
    }


//    @Log(title = "生成满意度调查表-事项文件记录")
//    @ApiOperation(value = "生成满意度调查表-事项文件记录")
    @GetMapping(value = "genMydTemplateFile")
    public HttpResult genMydTemplateFile(@RequestParam(value = "sqsxid",defaultValue = "") String sqsxid) {
        if (StringUtils.isBlank(sqsxid)){
            return HttpResult.error("事项参数为空！");
        }

        // 验证是否存在相关满意度调查问卷
        List<SpcySqsxLjzlMydBasic> mydList = spcySqsxLjzlMydBasicService.list(new LambdaQueryWrapper<SpcySqsxLjzlMydBasic>()
                .eq(SpcySqsxLjzlMydBasic::getFsqsxid, sqsxid));
        if (CollectionUtil.isEmpty(mydList)){
            return HttpResult.error("没有相关问卷调查表数据！");
        }

        Result result = spcyMyddcService.genMydTableTemplate(mydList.get(0));
        if (!result.isSuccess()){
            return HttpResult.error(result.getMessage());
        }

        return HttpResult.ok();
    }

}
